//Testing the Adventurer card
#include <stdio.h>
#include <stdlib.h>
#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"
#include "interface.h"
#include <math.h>
#include <assert.h>
#include <time.h>
#define CARD_FUNCTION 0
#define NUM_TESTS 5000

double genRandomNum();
void initPlayerCards(int player, int handSize, int discardSize, int deckSize, int *kingdom, struct gameState *state);

double genRandomNum(){
    return  (double)rand()/(double)RAND_MAX;
}

void initPlayerCards(int player, int handSize, int discardSize, int deckSize, int *kingdom, struct gameState *state){
    enum CARD card = adventurer;
    int kingdomCard, i;
    state->hand[player][0] = card; 
		
		//Set up Players Hand
        for(i=1; i<handSize-1; i++){
            if(rand() % 2 == 0){
                if(rand()%3 == 0)
                    state->hand[player][i] = copper;
                else if(rand()%3 ==1)
                    state->hand[player][i] = silver;
                else
                    state->hand[player][i] = gold;
            }
            else{
                kingdomCard = rand()%10;
                state->hand[player][i] = kingdom[kingdomCard];
            }
        }
    
		//Set up Player Deck
        for(i=0; i<deckSize; i++){
            if(rand() % 2 == 0){
                if(rand()%3 == 0)
                    state->deck[player][i] = copper;
                else if(rand()%3 ==1)
                    state->deck[player][i] = silver;
                else
                    state->deck[player][i] = gold;
            }
            else{
                kingdomCard = rand()%10;
                state->deck[player][i] = kingdom[kingdomCard];
            }
        }

		//Set up Player Discard
		for(i=0; i<deckSize; i++){
			if(rand() % 2 == 0){
				if(rand()%3 == 0)
					state->discard[player][i] = copper;
				else if(rand()%3 ==1)
					state->discard[player][i] = silver;
				else
					state->discard[player][i] = gold;
			}
			else{
				kingdomCard = rand()%10;
				state->discard[player][i] = kingdom[kingdomCard];
			}
		}
}

int main(){
    srand(time(NULL));
    struct gameState *state = malloc(sizeof(struct gameState));
    assert(state != 0);
    int numPlayers, drawnTreasure, choice1, choice2, choice3, handPos, n, init, test, player;
    enum CARD first = adventurer;
    enum CARD last = treasure_map;
    int bonus = 0;
    int randomSeed = 300;
    int kingdomCards[NUM_K_CARDS];

    if(CARD_FUNCTION == 0){
        for(n=0; n<=NUM_TESTS; n++){
            selectKingdomCards(randomSeed, kingdomCards);
            numPlayers = floor(genRandomNum()*(MAX_PLAYERS+1));
            init = initializeGame(numPlayers, kingdomCards, randomSeed, state);
            if(init != 0){
                continue;
            }
            drawnTreasure = floor(genRandomNum()*10);
            player = floor(genRandomNum()*numPlayers);
            state->whoseTurn = player;
            state->handCount[player] = floor(genRandomNum()*MAX_HAND)+1;
            if(MAX_DECK-state->handCount[player] < 0) state->deckCount[player] = 0;
            else state->deckCount[player] = floor(genRandomNum()*(MAX_DECK-(state->handCount[player])));
            if(MAX_DECK-(state->handCount[player])-(state->deckCount[player]) < 0) state->discardCount[player] =0;
            else state->discardCount[player] = floor(genRandomNum()*(MAX_DECK-(state->handCount[player])-(state->deckCount[player])));
            initPlayerCards(player, state->handCount[player], state->discardCount[player], state->deckCount[player], kingdomCards, state);
            test = adventurerCard(drawnTreasure, player, state);
            
        }
        printf("All tests have completed successfully.\n");
        
    }
    else{
        for(n=0; n<=NUM_TESTS; n++){
            selectKingdomCards(randomSeed, kingdomCards);
            numPlayers = floor(genRandomNum()*(MAX_PLAYERS+1));
            init = initializeGame(numPlayers, kingdomCards, randomSeed, state);
            if(init != 0){
                continue;
            }
            drawnTreasure = floor(genRandomNum()*6);
            player = floor(genRandomNum()*numPlayers);
            state->whoseTurn = player;
            state->handCount[player] = floor(genRandomNum()*MAX_HAND)+1;
            if(MAX_DECK-state->handCount[player] < 0) state->deckCount[player] = 0;
            else state->deckCount[player] = floor(genRandomNum()*(MAX_DECK-(state->handCount[player])));
            if(MAX_DECK-(state->handCount[player])-(state->deckCount[player]) < 0) state->discardCount[player] =0;
            else state->discardCount[player] = floor(genRandomNum()*(MAX_DECK-(state->handCount[player])-(state->deckCount[player])));
            initPlayerCards(player, state->handCount[player], state->discardCount[player], state->deckCount[player], kingdomCards, state);
            handPos = (genRandomNum()*state->handCount[player]);
            choice1 = (genRandomNum() *(last-first))+first;
            choice2 = (genRandomNum() *(last-first))+first;
            choice3 = (genRandomNum() *(last-first))+first;
            cardEffect(first, choice1, choice2, choice3, state, handPos, &bonus);
        }
    }
    printf("All tests have completed successfully.\n");
    return 0;
}